<?php
namespace app\index\model;
use think\Model;
use think\Request;

/**
 * Class Evalute
 * 评论模型
 */
class Evalute extends Model
{
    protected $autoWriteTimestamp = 'datetime';

    /**
     * @desc  获取所有的主评论信息
     * @return $arr 所有的主评论
     */
    public static function evalutes()
    {
        $re=self::alias('a')
            ->join('user b','a.userid=b.id','LEFT')
            ->field('a.*,b.name as user')
            ->order('create_time desc')
            ->select();
        $arr=[];
        foreach($re as $k)
        {
            $arr[]=$k->toArray();
        }
        return $arr;
    }

    /**
     * @desc  获取指定id 的主评论信息
     * @param $id  主评论的id
     * @return $arr  返回指定id的主评论信息
     */
    public static function getEvaluteById($id)
    {
        $re=self::alias('a')
            ->join('user b','a.userid=b.id')
            ->where('a.id',$id)
            ->field('a.*,b.name as name,b.photo as userphoto')
            ->find();
        $arr=$re->toArray();
        return $arr;

    }
    public static function delEvaluteById($id)
    {
        try{
            $re=self::get($id)->getData();
            self::startTrans();
            self::destroy($id);
            Evalutedesc::where('replyid',$id)->delete();
            if($re['filetype']=='image'){
                $filepath=ROOT_PATH.'public\uploadEvalute\image\\'.$re['filename'];
            }else{
                $filepath=ROOT_PATH.'public\uploadEvalute\video\\'.$re['filename'];
            }
            if(is_file($filepath)){
                unlink($filepath);
            }
            self::commit();
            return show(1,'删除成功');
        }catch (Exception $exception){
            self::rollback();
            return show(0,'删除失败');
        }
    }
    public function getFileNameAttr($value)
    {
        if(strripos($value,'mp4')){
            $photo='http://'.$_SERVER['SERVER_NAME'].'/shop/shop/public/uploadEvalute/video/'.$value;
        }else{
            $photo='http://'.$_SERVER['SERVER_NAME'].'/shop/shop/public/uploadEvalute/image/'.$value;
        }
        return $photo;
    }
    //评论总数
    public function zong(){
        $res=$this->field('count(id) as count ')->find();
        return $res['count'];
    }
}
 ?>
